{extend name="platform/new_base" /}
{block name="resource"}

{/block}
{block name="main"}
    <style>
        .w-60{width:60% !important;}
    </style>
        <!-- page -->
        <form class="form-horizontal pt-15 form-validate widthFixedForm">

            <div class="form-group">
                <label class="col-md-2 control-label"><span class="text-bright">*</span>规格名称</label>
                <div class="col-md-5">
                    <input type="text" class="form-control" id="spec_name" name="spec_name" required autocomplete="off">
                    <p class="help-block mb-0">例如：颜色、尺寸、型号等</p>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label"><span class="text-bright">*</span>规格值</label>
                <div class="col-md-5">
                    <textarea class="form-control valid" rows="8" id="spec_item" aria-invalid="false" required></textarea>
                    <p class="help-block">一行代表一个规格值，多个规格值回车换行添加</p>
                </div>
            </div>

            <!--<div class="form-group">
                <label class="col-md-2 control-label">关联品类</label>
                <div class="col-md-8" style="width: 740px">
                    <div class="transfer-box">
                        <div class="item">
                            <div class="transfer-title">
                                <div class="checkbox line-1-ellipsis">
                                    <label><input type="checkbox" name="brandAllCheck" value="" autocomplete="off">未选品类</label>
                                </div>
                            </div>
                            <div class="transfer-search">
                                <div class="transfer-search-div padding-10" style="padding-bottom: 0">
                                    <input type="text" class="form-control" placeholder="请输入品类名称" id="brand_txt">
                                    <i class="icon icon-custom-search search_button" id="brand_search"></i>
                                </div>
                            </div>
                            <div id="unbrand_id" class="heights">
                                <div class="checkbox">
                                        <label class="category-pr">
                                            <span class="line-1-ellipsis ellipsis-span"><input type="checkbox" name="un_brandid" value="11" data_name="班尼路2">班尼路2</span>
                                            <div class="category-pos">
                                                <div class="category-arrow">
                                                    <p>对应分类：</p>
                                                    <p>服装>男装>上衣</p>
                                                    <p>服装>男装>上衣</p>
                                                    <p>服装>男装>上衣</p>
                                                    <p>服装>男装>上衣</p>
                                                    <p>服装>男装>上衣</p>
                                                </div>
                                            </div>
                                        </label>
                                </div>
                                <div class="checkbox">
                                        <label class="category-pr">
                                            <span class="line-1-ellipsis ellipsis-span"><input type="checkbox" name="un_brandid" value="11" data_name="班尼路2">班尼路222</span>
                                            <div class="category-pos">
                                                <div class="category-arrow">
                                                    <p>对应分类：</p>
                                                    <p>服装>男装>上衣</p>
                                                    <p>服装>男装>上衣</p>
                                                    <p>服装>男装>上衣</p>
                                                    <p>服装>男装>上衣</p>
                                                    <p>服装>男装>上衣</p>
                                                </div>
                                            </div>
                                        </label>
                                </div>
                            </div>
                        </div>
                        <div class="item">
                            <div class="transfer-title">
                                <div class="checkbox line-1-ellipsis">
                                    已选品类
                                </div>
                            </div>
                            <div class="transfer-search">
                                <div class="transfer-search-div padding-10" style="padding-bottom: 0">
                                    <input type="text" class="form-control" placeholder="请输入品类名称" id="brand_txt_selected" autocomplete="off">
                                    <i class="icon icon-custom-search search_button_selected"></i>
                                </div>
                            </div>
                            <div id="brand_id" class="heights">
                            </div>
                        </div>
                    </div>
                </div>
            </div>-->

            <div class="form-group">
                <label class="col-md-2 control-label">关联品类</label>
                <div class="col-md-5">
                    <div class="checkbox_three">
                        {volist name="attribute_list" id="vo"}
                        <label class="checkbox-inline category-pr">
                            <input type="checkbox" name="attr_id" id="{$vo.attr_id}" value="{$vo.attr_id}">{$vo.attr_name}
                            <div class="category-pos">
                                <div class="category-arrow">
                                    <p>对应分类：</p>
                                    {volist name="vo['categorys']" id="v1"}
                                    <p>{$v1['category_names']}</p>
                                    {/volist}
                                </div>
                            </div>
                        </label>
                        {/volist}

                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label">规格排序</label>
                <div class="col-md-5">
                    <input type="number" class="form-control" id="sort" name="sort" autocomplete="off">
                    <p class="help-block mb-0">规格排序，数字越大越靠前。</p>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">是否启用</label>
                <div class="col-md-5">
                    <div class="switch-inline">
                        <input type="checkbox" id="is_visible">
                        <label for="is_visible" class=""></label>
                    </div>
                </div>
            </div>
            
            <div class="form-group"></div>
            <div class="form-group">
                <label class="col-md-2 control-label"></label>
                <div class="col-md-8">
                    <button class="btn btn-primary add_spec" type="submit">添加</button>
                    <a href="javascript:history.go(-1);" class="btn btn-default">返回</a>
                </div>
            </div>

        </form>

        <!-- page end -->

{/block}


{block name="script"}
<script type="text/javascript">

    require(['util'],function(util){



        function checkRepeat(arr){
            var hash = {};
             for(var i in arr) {
                 if(hash[arr[i]])
                     return true;
                 hash[arr[i]] = true;
             }
             return false;
         }
        //添加规格
        var flag = false;
        util.validate($('.form-validate'),function(form) {
            var spec_item = $('#spec_item').val();
            if(!spec_item){
                util.message('请填写规格值！', 'danger');
                return false;
            }
            var arr = spec_item.split(/[(\r\n)\r\n]+/);
            if(checkRepeat(arr))
            {
                util.message('规格值不能重复！','danger');
                return false;
            }      
            if(arr.length == 0){
                util.message('请填写规格值！','danger');
                return false;
            }
            var spec_value_str = '';
            for(var i = 0; i < arr.length; i++){
                if(arr[i]){
                    spec_value_str += ',' + arr[i];
                }
            }
            spec_value_str = spec_value_str.substr(1);
            var spec_name = $("input[name='spec_name']").val();
            var attr_id =[];    
            $('input[name="attr_id"]:checked').each(function(){    
                attr_id.push($(this).val());    
            });
            var sort = $("input[name='sort']").val();
            if ($("#is_visible").prop("checked")) {
                var is_visible = 1;
            } else {
                var is_visible = 0;
            }
            if(spec_value_str == ''){
                util.message("请填写规格值",'danger');
                return false;
            }
            if(flag){
                return false;
            }
            flag = true;
            $.ajax({
                type : "post",
                url : "{:__URL('PLATFORM_MAIN/goods/addgoodsspec')}",
                data : {
                    'spec_name' : spec_name,
                    'sort' : sort,
                    'is_visible' : is_visible,
                    'spec_value_str' : spec_value_str,
                    'attr_id': attr_id,
                },
                success : function(data) {
                    if (data["code"] > 0) {
                        util.message('添加成功', "success","{:__URL('PLATFORM_MAIN/goods/goodsspeclist')}");
                    } else {
                        util.message('添加失败！' + data['message'], "danger");
                        flag = false;
                    }
                }
            });

        })



    })
</script>
{/block}